package com.example.forum.service.impl;

import com.example.forum.dao.DiscussPostDao;
import com.example.forum.domain.DiscussPost;
import com.example.forum.domain.Page;
import com.example.forum.domain.User;
import com.example.forum.service.DiscussPostService;
import com.example.forum.utils.HostHolder;
import com.example.forum.utils.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.Date;
import java.util.List;

@Service
public class DiscussPostServiceImpl implements DiscussPostService {
    @Autowired
    private DiscussPostDao disCussPostDao;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Override
    public List<DiscussPost> findAll(String userId,int start,int size) {
        return disCussPostDao.findAll(userId,start,size);
    }

    //查询总记录数
    @Override
    public Page findAllCount(String userId) {
        Integer totalCount = disCussPostDao.findAllCount(userId);
        Page page=new Page();
        page.setTotalCount(totalCount);
        return page;
    }

    @Override
    public void addDiscussPost(String title,String text) {
        DiscussPost discussPost=new DiscussPost();
        User user = hostHolder.getUser();
        discussPost.setUserId(user.getId()+"");
        //转义html标签
        title = HtmlUtils.htmlEscape(title);
        text = HtmlUtils.htmlEscape(text);
        //过滤敏感字符
        title = sensitiveFilter.filter(title);
        text = sensitiveFilter.filter(text);
        discussPost.setTitle(title);
        discussPost.setContent(text);
        discussPost.setType(0);
        discussPost.setStatus(0);
        discussPost.setCreateTime(new Date());
        discussPost.setCommentCount(0);
        discussPost.setScore(0);
        disCussPostDao.addDiscussPost(discussPost);
    }

    @Override
    public DiscussPost findById(int id) {
        return disCussPostDao.findById(id);
    }
}
